import { URL, fileURLToPath } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

import UnoCSS from 'unocss/vite'
import autoprefixer from 'autoprefixer'
// import PostcssPxToViewport from "postcss-px-to-viewport-8-plugin-fix";
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: '0.0.0.0', // 设置为 0.0.0.0 或者 true 将监听所有地址，包括局域网和公网地址
    port: 8080,
    proxy: {
      '/api': {
        target: 'https://www.sxbqeh.com.cn',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/api/, ''),
      }
    }
  },
  plugins: [
    // https://cn.vuejs.org/guide/extras/web-components.html
    vue({
      template: {
        compilerOptions: {
          isCustomElement: tag => tag.includes('swiper-')
        }
      }
    }),
    vueJsx(),
    UnoCSS(),
    Components({
      dirs: ['src/components'],
      resolvers: [VantResolver()],
    }),
    AutoImport({
      include: [
        /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
        /\.vue$/,
        /\.vue\?vue/, // .vue
        /\.md$/, // .md
      ],
      imports: [
        'vue',
        'vue-router',
        'pinia',
        '@vueuse/core',
        {
          '@vueuse/router': ['useRouteHash', 'useRouteParams', 'useRouteQuery']
        }
      ],
      dirs: ['src/api/**', 'src/hooks/**', 'src/stores/**', 'src/utils/**']
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  css: {
    postcss: {
      plugins: [
        autoprefixer({
          overrideBrowserslist: [
            'iOS >= 9',
            'Android >= 4.4',
            '> 1%',
            'last 2 versions'
          ]
        })
        // PostcssPxToViewport({
        //   viewportWidth: 375
        // })
      ]
    }
  }
})
